<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="isAllComplete"/>
    </label>
    <span>
      <span>已完成{{completeCount}}</span> / 全部{{totalCount}}
    </span>
    <button class="btn btn-danger" @click="clearSelectTodos">清除已完成任务</button>
  </div>
</template>

<script>
  import {mapGetters} from "vuex";

  export default {
    name: "todo-footer",
    computed: {
      //为组件创建计算属性以返回 getter 的返回值。
      ...mapGetters(['totalCount', 'completeCount']),
      isAllComplete:{
        /*
        * 计算属性将被混入到 Vue 实例中。所有 getter 和 setter 的 this 上下文自动地绑定为 Vue 实例。
        * */
        get(){
          return this.$store.getters.isAllComplete
        },
        set(value){
          //触发actions: 是否全选
          this.$store.dispatch('selectAllTodos', value);
        }
      }
    },
    methods: {
      clearSelectTodos: function () {
        this.$store.dispatch('clearSelectTodos')
      }
    }
  }
</script>

<style scoped>
  /*footer*/
  .todo-footer {
    height: 40px;
    line-height: 40px;
    padding-left: 6px;
    margin-top: 5px;
  }

  .todo-footer label {
    display: inline-block;
    margin-right: 20px;
    cursor: pointer;
  }

  .todo-footer label input {
    position: relative;
    top: -1px;
    vertical-align: middle;
    margin-right: 5px;
  }

  .todo-footer button {
    float: right;
    margin-top: 5px;
  }

</style>
